<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Carbon\Carbon;

class TicketAvailableNotification extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * 余票信息
     *
     * @var array
     */
    public $availableTickets;

    /**
     * 查询时间
     *
     * @var string
     */
    public $queryTime;

    /**
     * Create a new message instance.
     *
     * @param array $availableTickets
     */
    public function __construct($availableTickets)
    {
        $this->availableTickets = $availableTickets;
        $this->queryTime = Carbon::now()->toDateTimeString();
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('江西博物馆余票通知 - ' . date('Y-m-d H:i:s'))
                    ->view('emails.ticket-available')
                    ->with([
                        'availableTickets' => $this->availableTickets,
                        'queryTime' => $this->queryTime,
                        'totalAvailable' => array_sum(array_column($this->availableTickets, 'available_count'))
                    ]);
    }
}